package com.sxz.mall.product.web;

import com.sxz.mall.product.entity.CategoryEntity;
import com.sxz.mall.product.service.CategoryService;
import com.sxz.mall.product.vo.Catalog2VO;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @GetMapping({"/", "/index", "/home"})
    public String index(Model model) {

        // 获取左侧一级分类
        List<CategoryEntity> list = categoryService.getLevel1Category();
        model.addAttribute("categorys", list);

        // 默认会返回 templates下面的 index.html
        // thymeleaf默认的前后缀
        return "index";
    }

    @ResponseBody
    @RequestMapping("/index/catalog.json")
    public Map<String, List<Catalog2VO>> getCatalog2JSON() {

        Map<String, List<Catalog2VO>> map = categoryService.getCatalog2JSON();
        return map;
    }

    @GetMapping("/writer")
    @ResponseBody
    public String writeValue() {
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("wr-lock");

        RLock rLock = readWriteLock.writeLock();
        String s = null;

        // 加写锁
        rLock.lock();
        try {
            System.out.println("加写锁成功......");
            s = UUID.randomUUID().toString();
            stringRedisTemplate.opsForValue().set("msg", s);
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            rLock.unlock(); // 释放锁
        }
        return s;
    }

    @GetMapping("/reader")
    @ResponseBody
    public String readValue() {
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("wr-lock");
        RLock rLock = readWriteLock.readLock();

        String s1 = null;
        // 加读锁
        rLock.lock();
        try {
            System.out.println("加读锁成功......");
            s1 = stringRedisTemplate.opsForValue().get("msg");
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            rLock.unlock(); // 释放锁
        }
        return s1;
    }

    @GetMapping("/lockDoor")
    @ResponseBody
    public String lockDoor() {
        // 添加闭锁  有5个小任务需要完成
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.trySetCount(5);
        try {
            door.await(); // 保持等待
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "熄灯关门.....";
    }

    @GetMapping("/goHome/{id}")
    @ResponseBody
    public String goHome(@PathVariable Long id) {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown();
        return id + "下班回家";
    }


    @GetMapping("/park")
    @ResponseBody
    public String park() {
        RSemaphore semaphore = redissonClient.getSemaphore("park");
        Boolean b = null;
        try {
//            semaphore.acquire(); //获取信号量，阻塞到获取成功
            b = semaphore.tryAcquire();// 返回成功还是失败，不等
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "停车是否成功....:" + b;
    }

    @GetMapping("/release")
    @ResponseBody
    public String release() {
        RSemaphore semaphore = redissonClient.getSemaphore("park");
        semaphore.release();
        return "释放了一个车位";
    }
}
